//
//  TimeSpanSettingView.m
//  BabyHelper
//
//  Created by  thomas on 11-2-21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TimeSpanSettingView.h"


@implementation TimeSpanSettingView

@synthesize pickerView, dateFormatter,startDate,endDate;

- (void)viewDidLoad
{
	self.title = NSLocalizedString(@"Custom Report", @"");
	
/*	
	//set the default date is today 00:00
	self.startDate = [[NSDate alloc]init];
	self.endDate   = [[NSDate alloc]init];
	NSDateFormatter *tmpFormatter = [[NSDateFormatter alloc] init];
	[tmpFormatter setDateFormat:DATE_FORMAT];
	NSString *strTime = [tmpFormatter stringFromDate:[NSDate date]];
    self.startDate = [tmpFormatter dateFromString:strTime];
	self.endDate   = [tmpFormatter dateFromString:strTime];
	[tmpFormatter release];
*/		
    self.startDate = [NSDate date];
	self.endDate   = [NSDate date];

	self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	[self.dateFormatter setDateFormat:DATE_FORMAT];
	//[self.dateFormatter setDateStyle:NSDateFormatterShortStyle];
	//[self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];

	[super viewDidLoad];
	UIBarButtonItem *item = [[UIBarButtonItem alloc]
								 initWithTitle:NSLocalizedString(@"View", @"")
								 style:UIBarButtonItemStyleBordered 
								 target:self
								 action:@selector(toggleReport)		
								 ];
	self.navigationItem.rightBarButtonItem = item;
	[item release];
	
	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];
	
}

- (void)viewDidUnload
{
	self.dateFormatter = nil;
	self.startDate = nil;
	self.endDate = nil;
	[super viewDidUnload];
}

- (void)dealloc
{	
	[pickerView release];
	[dateFormatter release];
	[startDate release];
	[endDate release];
	[super dealloc];
}

#pragma mark - UITableView delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 2;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];
	self.pickerView.date = [self.dateFormatter dateFromString:targetCell.detailTextLabel.text];
	
	NSLog(@"%@",targetCell.detailTextLabel.text);
	NSLog(@"%@",[self.pickerView.date description]);
	// check if our date picker is already on screen
	if (self.pickerView.superview == nil)
	{
		[self.view.window addSubview: self.pickerView];
		
		// size up the picker view to our screen and compute the start/end frame origin for our slide up animation
		//
		// compute the start frame
		CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
		CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
		CGRect startRect = CGRectMake(0.0,
									  screenRect.origin.y + screenRect.size.height,
									  pickerSize.width, pickerSize.height);
		self.pickerView.frame = startRect;
		
		// compute the end frame
		CGRect pickerRect = CGRectMake(0.0,
									   screenRect.origin.y + screenRect.size.height - pickerSize.height - 49,
									   pickerSize.width,
									   pickerSize.height);
		// start the slide up animation
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.3];
		
		// we need to perform some post operations after the animation is complete
		[UIView setAnimationDelegate:self];
		
		self.pickerView.frame = pickerRect;
		
		// shrink the table vertical size to make room for the date picker
		CGRect newFrame = self.tableView.frame;
		newFrame.size.height -= self.pickerView.frame.size.height;
		self.tableView.frame = newFrame;
		[UIView commitAnimations];
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *kCustomCellID = @"CustomCellID";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
	if (cell == nil)
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCustomCellID] autorelease];
	}
	
	switch (indexPath.row) {
		case 0:
			cell.textLabel.text = NSLocalizedString(@"Start Date", @"");
			cell.detailTextLabel.text = [self.dateFormatter stringFromDate:[NSDate date]];
			break;
		default:
			cell.textLabel.text = NSLocalizedString(@"End Date", @"");
			cell.detailTextLabel.text = [self.dateFormatter stringFromDate:[NSDate date]];
			break;
	}
	
	cell.backgroundColor = [UIColor colorWithRed:cellBackgroundReportRed green:cellBackgroundReportGreen 
											blue:cellBackgroundReportBlue alpha:cellBackgroundReportAlpha];

	
	return cell;
}

- (void)slideDownDidStop
{
	// the date picker has finished sliding downwards, so remove it
	[self.pickerView removeFromSuperview];
}

- (IBAction)dateAction:(id)sender
{
	NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
	UITableViewCell *cell  = [self.tableView cellForRowAtIndexPath:indexPath];
	cell.detailTextLabel.text = [self.dateFormatter stringFromDate:self.pickerView.date];
	
	switch (indexPath.row) {
		case 0:
			self.startDate = self.pickerView.date;
			break;
		case 1:
			self.endDate   = self.pickerView.date;
			break;
		default:
			break;
	}
}

-(void)toggleReport{

	if( [self.startDate compare:self.endDate] == NSOrderedDescending )
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"")
														message:NSLocalizedString(@"Make sure start date is early than end date", @"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
		return;
	}
	else {
		CustomReportView *report = [[CustomReportView alloc] initWithNibName:@"CustomReportView" bundle:nil];
		report.startDate = self.startDate;
		
		
		if ( [self.startDate compare:self.endDate] == NSOrderedSame ) 
		{
			NSTimeInterval secondsIn24Hours = 24 * 60 * 60;			
			report.endDate = [self.endDate addTimeInterval:secondsIn24Hours];
		}
		else
			report.endDate = self.endDate;
	
		[self.navigationController pushViewController:report animated:YES];
		[report release];		
	}
}
	
- (void)viewWillDisappear:(BOOL)animated{
	
	CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
	CGRect endFrame = self.pickerView.frame;
	endFrame.origin.y = screenRect.origin.y + screenRect.size.height;
	
	// start the slide down animation
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3];
	
	// we need to perform some post operations after the animation is complete
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(slideDownDidStop)];
	
	self.pickerView.frame = endFrame;
	[UIView commitAnimations];
	
	// grow the table back again in vertical size to make room for the date picker
	CGRect newFrame = self.tableView.frame;
	newFrame.size.height += self.pickerView.frame.size.height;
	self.tableView.frame = newFrame;
	
	[super viewWillDisappear:animated];
}
@end

